import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

import java.util.Collections;

/**
 * @author SEG
 */
public class TGnerate {
    @Value("${spring.datasource.url}")
    public String dbUrl = "jdbc:mysql://127.0.0.1:3306/tef4_opc?useSSL=true&serverTimezone=GMT%2B8&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useAffectedRows=true";
    @Value("${spring.datasource.url}")
    private String username = "root";
    @Value("${spring.datasource.password}")
    private String password = "tef4-123456";

    @Test
    public void test() {
        FastAutoGenerator.create(dbUrl, username, password)
                .packageConfig(builder -> {
                    builder.parent("com.opc") // 父包模块名
                            .controller("controller")   //Controller 包名 默认值:controller
                            .entity("entity")           //Entity 包名 默认值:entity
                            .service("service")         //Service 包名 默认值:service
                            .mapper("mapper")           //Mapper 包名 默认值:mapper
                            .other("model")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\workspace\\tef4_opc\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径//默认存放在mapper的xml下
                })
                .strategyConfig(builder -> {
                    builder.addInclude("t_device") // 设置需要生成的表名 可边长参数“user”, “user1”
                            .addTablePrefix("t_", "gms_") // 设置过滤表前缀
                            .serviceBuilder()//service策略配置
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()// 实体类策略配置
                            .idType(IdType.AUTO)//主键策略  雪花算法自动生成的id
                            .addTableFills(new Column("create_time", FieldFill.INSERT)) // 自动填充配置
                            .addTableFills(new Property("update_time", FieldFill.INSERT_UPDATE))
                            .enableLombok() //开启lombok
                            .logicDeleteColumnName("deleted")// 说明逻辑删除是哪个字段
                            .enableTableFieldAnnotation()// 属性加上注解说明
                            .controllerBuilder() //controller 策略配置
                            .formatFileName("%sController")
                            .enableRestStyle() // 开启RestController注解
                            .mapperBuilder()// mapper策略配置
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()//@mapper注解开启
                            .formatXmlFileName("%sMapper");
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


}
